package com.hava.web.bean;

import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.annotation.Bean;

import java.lang.reflect.Field;

/**
 * Created by zhanpeng on 15-1-19.
 */
public class InitSpringBean implements InitializingBean,DisposableBean {

    private String name;

    @Override
    public void destroy() throws Exception {
        System.out.println(this.getClass().getName() + " destroy...");
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println(this.getClass().getName() + " init...");
        
        //由反射来获取property的属性名称、属性值
        Field[] fields = this.getClass().getDeclaredFields();
        for(Field field : fields)
        {
            //field.setAccessible(true); //这里由于声明的property为public，所以不需要对field开启访问权限
            System.out.println("property[" + field.getName() + "]:" + field.get(this));
        }
    }
    
    //从Spring配置Bean的property配置中获取对应属性的值，必须setter,getter
    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }
}